package com.springboot.shanhexin.controller;

import com.springboot.shanhexin.entity.Page;
import com.springboot.shanhexin.entity.tourist;
import com.springboot.shanhexin.service.touristService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class touristCtrl {

    @Autowired
    private touristService touristService;

    //分页显示全部
    @RequestMapping("/tourist")
    public String tourist(Model model, String pageNow, String nosearch) {
        int totalCount = 0;
        totalCount = touristService.getCount();
        List<tourist> touristList=new ArrayList<tourist>();
        Page page=null;
        if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            touristList=touristService.findByPage(page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            touristList=touristService.findByPage(page.getStartPos(), page.getPageSize());
        }
        model.addAttribute("page", page);
        model.addAttribute("top", "top_tourist");
        model.addAttribute("show_left", "left_tourist");
        model.addAttribute("left", "left_y_tourist");
        model.addAttribute("mypage", "tourist");
        model.addAttribute("tourist", touristList);
        if(nosearch != null){
            model.addAttribute("nosearch", nosearch);
        }
        return "houtai_tourist";
    }

    //分页显示全部搜索
    @RequestMapping("/searchtourist")
    public String searchtourist(Model model, String pageNow,int types,String keyword) {
        int totalCount = 0;
        String keyword_new="b.name";
        if(types == 1) {
            keyword_new = "b.name";
        }else if (types == 2) {
            keyword_new = "a.title";
        }
        totalCount = touristService.getsearchCount(keyword_new,keyword);
        List<tourist> touristList=new ArrayList<tourist>();
        Page page=null;
        if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            touristList=touristService.findsearchByPage(keyword_new,keyword,page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            touristList=touristService.findsearchByPage(keyword_new,keyword,page.getStartPos(), page.getPageSize());
        }
        if(touristList != null && totalCount != 0){
            model.addAttribute("page", page);
            model.addAttribute("top", "top_tourist");
            model.addAttribute("show_left", "left_tourist");
            model.addAttribute("left", "left_y_tourist");
            model.addAttribute("mypage", "search&types="+types+"&keyword="+keyword+"");
            model.addAttribute("tourist", touristList);
            return "houtai_tourist";
        }else{
            model.addAttribute("nosearch", "nosearch");
            return "redirect:/tourist?nosearch=nosearch";
        }
    }

    //添加
    //接收并返回json格式数据
    @RequestMapping("/addtourist")
    @ResponseBody
    public String addtourist(tourist tourist) {
        touristService.add(tourist);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "1");
        return jsonObject.toString();
    }

    //更改
    @RequestMapping("/updatetourist")
    public String updatetourist(tourist tourist) {
        touristService.update(tourist);
        return "redirect:/tourist";
    }

    //删除
    @RequestMapping("/deletetourist")
    public String deletetourist(int userid) {
        touristService.delete(userid);
        return "redirect:/tourist";
    }

    //跳转到更改页面
    @RequestMapping("/to_updatetourist")
    public String to_updatetourist(Model model, int userid) {
        List<tourist> touristList=new ArrayList<tourist>();
        touristList = touristService.getUserone(userid);
        model.addAttribute("top", "top_tourist");
        model.addAttribute("show_left", "left_updatetourist");
        model.addAttribute("left", "left_y_tourist");
        model.addAttribute("mypage", "tourist");
        model.addAttribute("tourist", touristList);
        return "houtai_updatetourist";
    }

    //跳转到添加页面
    @RequestMapping("/to_addtourist")
    public String to_addtourist(Model model) {
        model.addAttribute("top", "top_tourist");
        model.addAttribute("show_left", "left_addtourist");
        model.addAttribute("left", "left_y_tourist");
        model.addAttribute("mypage", "tourist");
        return "houtai_addtourist";
    }

}
